package com.lantosec.hmrp.modules.internal.service.impl;

import com.lantosec.hmrp.core.exception.AppException;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.internal.entity.*;
import com.lantosec.hmrp.modules.internal.mapper.InternalNoteMapper;
import com.lantosec.hmrp.modules.internal.service.IInternalNoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.internal.vo.InternalNoteVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评价记录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-23
 */
@Service
@Primary
@Log4j2
public class InternalNoteServiceImpl extends ServiceImpl<InternalNoteMapper, InternalNote> implements IInternalNoteService {
    @Autowired
    private InternalItemServiceImpl internalItemService;

    @Autowired
    private  InternalLevelServiceImpl internalLevelService;

    @Autowired
    private InternalTaskDetailServiceImpl internalTaskDetailService;

    @Override
    public InternalNoteVO get(String internalNoteId) {
        InternalNote internalNote = this.getById(internalNoteId);
        InternalNoteVO internalNoteVO = internalNote.convertToVO();
        //查询评价项目名称
        InternalItem internalItem = internalItemService.getById(internalNoteVO.getInternalItemId());
        //查询考评等级名称
        InternalLevel internalLevel = internalLevelService.getById(internalNoteVO.getInternalLevelId());
        internalNoteVO.setInternalItemName(internalItem.getInternalItemName());
        internalNoteVO.setInternalLevelName(internalLevel.getInternalLevelName());
        return internalNoteVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void taskCommit(List<InternalNoteVO> internalNoteList) {
        if(CollectionUtils.isEmpty(internalNoteList)){
            //return;
            throw new AppException("提交记录不能为空！");
        }
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        List<InternalNote> saveList = new ArrayList<>();
        //新增考评记录
        for (InternalNoteVO internalNoteVO:internalNoteList) {
            internalNoteVO.setExecutorUserId(userId);
            internalNoteVO.setCreateTime(DateUtils.nowDate());
            internalNoteVO.setSubmitTime(DateUtils.nowDate());
            InternalNote internalNote = internalNoteVO.convertToEntity();
            saveList.add(internalNote);
        }
        saveBatch(saveList);

        //更新internal_task_detail表，对被考评对象完成考评提交，即更新被评价人的完成状态
        InternalTaskDetail internalTaskDetail = new InternalTaskDetail();
        internalTaskDetail.setInternalTaskDetailId(internalNoteList.get(0).getInternalTaskDetailId());
        internalTaskDetail.setFinished(1);
        internalTaskDetailService.updateById(internalTaskDetail);
        //查询当前评价人的评价人
        InternalTaskExecutor internalTaskExecutor = baseMapper.getInternalTaskExecutor(internalNoteList.get(0).getInternalTaskId(),userId);
        Integer targetTotalnum = internalTaskExecutor.getTargetTotalnum();
        Integer targetFinishednum = internalTaskExecutor.getTargetFinishednum();
        //如果已评价的被评价对象数小于被评价对象总数时 对已评价的被评价对象数+1
        if(targetFinishednum < targetTotalnum){
            targetFinishednum = targetFinishednum + 1;
            baseMapper.updateInternalTaskExecutor(internalTaskExecutor.getInternalTaskExecutorId(),targetFinishednum);
        }else {
            //更新评价任务中的完成评价人数+1
            baseMapper.updateInternalTask(internalTaskExecutor.getInternalTaskId());

        }
    }
}
